/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.security.controller;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import org.quartz.SchedulerException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.vacoor.nothing.common.misc.util.Locales;
import org.vacoor.nothing.common.util.StringUtils;
import org.vacoor.nothing.web.util.SettingsUtils;
import org.vacoor.nothing.security.nls.NlsSupport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.Map;
import java.util.Properties;

/**
 * @author vacoor
 */
@Controller
@RequestMapping("lang")
public class LangController {

    @RequestMapping
    public String index(String lang, Map<String, Object> model, HttpServletRequest req, HttpServletResponse resp) throws SchedulerException, ParseException {
        model.put("props", get(lang));
        return "/lang/lang";
    }

    public void test() {
        System.out.println("test");
    }

    @ResponseBody
    @RequestMapping("save")
    public Map<String, Object> save(@RequestParam Map<String, String> params) {
        String lang = params.remove("lang");
        Integer port = SettingsUtils.getInt("smtp.port");
        JavaMailSenderImpl sender;
        Properties props = new Properties();
        props.putAll(params);
        NlsSupport.save(Locales.parse(lang), props);
        return Collections.emptyMap();
    }

    @ResponseBody
    @RequestMapping("get")
    public Properties get(String lang) {
        return NlsSupport.get(Locales.parse(lang));
    }

    @ResponseBody
    @RequestMapping("upload")
    public Map<String, Object> upload(String lang, MultipartFile file) throws IOException {
        if (!StringUtils.hasText(lang) || file.isEmpty()) {
            return null;

        }
        Properties props = new Properties();
        props.load(new InputStreamReader(file.getInputStream(), Charset.forName("UTF-8")));
        NlsSupport.save(Locales.parse(lang), props);
        return ImmutableMap.<String, Object>of("success", true);
    }

    @RequestMapping("download")
    public void download(String lang, HttpServletResponse resp) throws IOException {
        String filename = "lang_" + lang + ".properties";
        resp.setHeader("Content-Disposition", "attachment; filename=\"" + new String(filename.getBytes("utf-8"), "iso8859-1") + "\"; filename*=utf-8''" + URLEncoder.encode(filename, "UTF-8"));
        Properties props = NlsSupport.get(Locales.parse(lang));
        props.store(new OutputStreamWriter(resp.getOutputStream(), Charsets.UTF_8), "Native language support - " + lang);
    }

    public static class LangPack {
        private String lang;
        private Properties nls;

        public String getLang() {
            return lang;
        }

        public void setLang(String lang) {
            this.lang = lang;
        }

        public Properties getNls() {
            return nls;
        }

        public void setNls(Properties nls) {
            this.nls = nls;
        }
    }
}
